package com.vitabuybuy.tv.dialog

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemCategory1Binding
import com.vitabuybuy.tv.databinding.PopupCategroyBinding
import com.vitabuybuy.tv.dto.FirstCategoryDto
import com.library.common.base.BindRecycAdapter
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 详情规则弹出框
 */
class CategroyPopup(
    context: Context,
    val categoryList:MutableList<FirstCategoryDto>,
    val onConfirm: (categroy: FirstCategoryDto) -> Unit
) : BasePopupWindow(context) {


    init {
        categroyAdapter.setNewData(categoryList)
    }

    private lateinit var mBinding: PopupCategroyBinding

    private lateinit var categroyAdapter: CategroyAdater

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_categroy)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupCategroyBinding


        categroyAdapter = CategroyAdater()
        mBinding.listview.apply {
            adapter = categroyAdapter
            addItemDecoration(RecycleViewDivider(context, DensityUtil.dip2px(1f)))
            layoutManager = LinearLayoutManager(context)
        }

        categroyAdapter.setOnItemClickListener { _, item, _ ->
            onConfirm(item)
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }


    class CategroyAdater : BindRecycAdapter<ItemCategory1Binding, FirstCategoryDto>() {
        override fun onBindViewHolder(
            mBinding: ItemCategory1Binding,
            itemData: FirstCategoryDto,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.itemData = itemData
        }

        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_category_1
        }
    }
}